package pers.youqu.search.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pers.youqu.common.constants.MQConstants;
import pers.youqu.search.service.IndexesService;
import pers.youqu.search.service.SearchService;

/**
 * @author Jason
 * @version 1.0.0
 * @date 2020/12/15 11:48
 * @Description TODO
 */
@Component
public class MQListener {
    @Autowired
    private IndexesService indexesService;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.QueueConstants.SEARCH_ITEM_UP, durable = "true"),
            exchange = @Exchange(name = MQConstants.ExchangeConstants.ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKeyConstants.ITEM_UP_KEY
    ))
    public void listenItemUp(Long spuId){
        if (spuId != null) {
            // 商品上架向索引库插入一条数据
            indexesService.saveGoodsById(spuId);
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MQConstants.QueueConstants.SEARCH_ITEM_DOWN, durable = "true"),
            exchange = @Exchange(name = MQConstants.ExchangeConstants.ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
            key = MQConstants.RoutingKeyConstants.ITEM_DOWN_KEY
    ))
    public void listenItemDown(Long spuId){
        if (spuId != null) {
            // 商品下架删除一条商品数据
            indexesService.deleteGoodsById(spuId);
        }
    }
}
